#pragma once
#include <map>
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>


namespace U33
{
	using namespace std;
	class word_counter
	{
	private:
		map<string, int> words;

		void read_from_file(string filename)
		{
			string str;
			try
			{
				ifstream fin(filename);
				while (!fin.eof())
				{
					fin >> str;
					if (words.find(str) == words.end())
						words.emplace(str, 1);
					else
						words[str]++;
				}
				fin.close();
			}
			catch (exception e)
			{
				cout << e.what() << endl;
			}
		}
	public:

		word_counter()
		{
		}

		~word_counter()
		{
		}

		static bool comp(const pair<string, int> &lop, const pair<string, int> &rop)
		{
			return lop.second < rop.second;
		}

		void print_max(string filename)
		{
			if (!words.empty())
				words.clear();
			read_from_file(filename);
			auto res = max_element(words.begin(), words.end(), comp);
			cout << res->first << ": " << res->second << endl;
		}
	};
}

